﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using weclever.Infrastructure;
using weclever.Navigation;
using HtmlAgilityPack;
using System.Diagnostics;

namespace weclever.Pages
{
    public class CityPage
    {
        private City LocationCity { get; set; }
        private string siteHtml = "";
        private HtmlNode node = null;
        private HtmlDocument doc = null;
        
        public string SiteHtml { get { return siteHtml; } }
        
        private IPageCommunication PageCommunication { get; set; }
                            
        public CityPage(IPageCommunication pageCommunication, City city)
        {
            LocationCity = city.Clone() as City;
            PageCommunication = pageCommunication;
            NavigateToPage(1);
            PositionLinks();
           // NumberPropositions();
        }
        /// <summary>
        /// find all positions and extract their names
        /// </summary>
        /// <returns>list of position's names</returns>
        public List<string> PositionLinks()
        {
            List<string> links = new List<string>();
            int posNumber = NumberPropositions();
            int page = 1; // first page was loaded previously
            while (links.Count < posNumber)
            {
                if (page > 1)
                {
                    NavigateToPage(page);
                }
                int posFound = 0;
                HtmlNodeCollection nodeCollection = node.SelectNodes("//div[@class]");
                foreach (var n in nodeCollection)
                {
                    //Debug.WriteLine(n.Attributes["class"].Value);
                    string value = n.GetAttributeValue("class", String.Empty);
                    if (!(value.Contains("weclever_act_title") || value.Contains("weclever_hor_act_title"))) { continue; }
                    HtmlNode subNode = n.SelectSingleNode("a");
                    if (subNode == null) { continue; }
                    String hrefValue = subNode.GetAttributeValue("href", String.Empty);
                    List<string> lst = SiteUrls.UrlParse(hrefValue);
                    if (lst.Count == 0) { continue; }
                    links.Add(lst.Last());
                    posFound++;
                }
                if (posFound == 0) { break; }
                page++; // next page
                
            }
            //HtmlNodeCollection nc = node.SelectNodes("//div[@class]/a");
                        
            return links;
        }
        /// <summary>
        /// provide working with pagination 
        /// </summary>
        /// <param name="pageNumber">page number</param>
        private void NavigateToPage(int pageNumber)
        {
            if (pageNumber == 1)
            {
                siteHtml = PageCommunication.NavigateToURL(SiteUrls.MakeAddress(LocationCity.NameEng));
            }
            else
            {
                siteHtml = PageCommunication.NavigateToURL(SiteUrls.MakeAddress(LocationCity.NameEng, pageNumber.ToString()));
            }
            doc = new HtmlDocument();
            doc.LoadHtml(siteHtml);
            node = doc.DocumentNode;
        }
        /// <summary>
        /// Return number of position in all categories from menu
        /// </summary>
        /// <returns></returns>
        private int NumberPropositions()
        {           
            HtmlNode curNode = doc.GetElementbyId("menu_first");
            HtmlNodeCollection nodeCollection = curNode.SelectNodes("a[@class='cat1_active nobg']/sup");
            if (nodeCollection != null && nodeCollection.Count > 0) // Count should be 1
            {
                int res;
                int.TryParse(nodeCollection[0].InnerText.Trim(), out res);
                return res;
            }
            return 0;
        }

        

    }
}
